#!/usr/bin/env python3
"""
诊断Key Features提取问题的脚本
对比页面HTML结构和我们的选择器逻辑
"""

import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from webdriver_manager.chrome import ChromeDriverManager
import json
import time

def diagnose_key_features_extraction():
    """诊断Key Features提取问题"""
    print("🔍 开始诊断Key Features提取问题...")
    
    # 设置Chrome选项
    chrome_options = Options()
    chrome_options.add_argument('--disable-blink-features=AutomationControlled')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--disable-gpu')
    chrome_options.add_argument('--window-size=1920,1080')
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    
    # 创建WebDriver
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=chrome_options)
    driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
    
    try:
        # 访问页面
        url = "https://www.ford.co.uk/shop/build-your-ford/ngc2/ice2/model?catalogId=WAEGB-CGE-2022-FocusMCAGBR202450&series=ACMAA_VS-DH&planId=c"
        print(f"🌐 访问: {url}")
        driver.get(url)
        
        # 等待页面加载
        print("⏳ 等待页面加载...")
        time.sleep(8)
        
        # 检查页面是否加载完成
        try:
            WebDriverWait(driver, 20).until(
                lambda d: d.execute_script("return document.readyState") == "complete"
            )
            print("✅ 页面基础加载完成")
        except:
            print("⚠️ 页面加载超时，继续尝试...")
        
        # 等待车型元素出现
        print("⏳ 等待车型元素出现...")
        try:
            WebDriverWait(driver, 15).until(
                EC.presence_of_element_located((By.CSS_SELECTOR, 'div[data-cy]'))
            )
            print("✅ 检测到data-cy元素")
        except:
            print("⚠️ 未检测到data-cy元素，继续尝试...")
        
        # 查找车型元素
        print("\n🎯 查找车型元素...")
        car_elements = driver.find_elements(By.CSS_SELECTOR, 'div[data-cy][class*="ProductTile"]')
        print(f"使用选择器 'div[data-cy][class*=\"ProductTile\"]' 找到 {len(car_elements)} 个车型元素")
        
        # 如果没找到，尝试其他选择器
        if not car_elements:
            print("🔍 尝试其他车型选择器...")
            alternative_selectors = [
                'div[data-cy]',
                'div[class*="ProductTile"]',
                'div[class*="product"]',
                'div[class*="tile"]'
            ]
            
            for selector in alternative_selectors:
                elements = driver.find_elements(By.CSS_SELECTOR, selector)
                print(f"  使用选择器 '{selector}' 找到 {len(elements)} 个元素")
                if elements:
                    for i, element in enumerate(elements[:10]):
                        try:
                            data_cy = element.get_attribute('data-cy')
                            class_attr = element.get_attribute('class')
                            text = element.text.strip()[:50] if element.text else ''
                            print(f"    [{i+1}] data-cy='{data_cy}' class='{class_attr}' text='{text}...'")
                        except:
                            pass
        
        titanium_element = None
        for element in car_elements:
            data_cy = element.get_attribute('data-cy')
            print(f"  - data-cy: {data_cy}")
            if data_cy == 'Titanium':
                titanium_element = element
                break
        
        if not titanium_element:
            print("❌ 未找到Titanium车型元素")
            return
        
        print("✅ 找到Titanium车型元素")
        
        # 查找View Specifications按钮
        print("\n🔍 查找View Specifications按钮...")
        try:
            view_specs_button = titanium_element.find_element(By.CSS_SELECTOR, 'button[data-cy="product-tile-overlay"]')
            print("✅ 找到View Specifications按钮")
            
            # 滚动到元素位置
            driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", view_specs_button)
            time.sleep(2)
            
            # 点击按钮 - 使用JavaScript直接点击
            print("🖱️  点击View Specifications按钮...")
            driver.execute_script("arguments[0].click();", view_specs_button)
            time.sleep(5)  # 增加等待时间
            
            print("✅ 按钮点击成功")
            
        except Exception as e:
            print(f"❌ 点击按钮失败: {e}")
            return
        
        # 分析页面中的所有可能的Key Features元素
        print("\n🔍 分析页面中的Key Features元素...")
        
        # 方法1：查找ul li元素（我们当前使用的方法）
        print("\n📋 方法1：使用 'ul li' 选择器")
        ul_li_elements = driver.find_elements(By.CSS_SELECTOR, 'ul li')
        print(f"找到 {len(ul_li_elements)} 个 ul li 元素")
        for i, element in enumerate(ul_li_elements[:20]):  # 只显示前20个
            try:
                text = element.text.strip()
                if text:
                    print(f"  [{i+1}] {text}")
            except:
                pass
        
        # 方法2：查找可能包含Key Features的其他元素
        print("\n📋 方法2：查找可能的Key Features容器")
        possible_selectors = [
            'div[class*="feature"]',
            'div[class*="specification"]',
            'div[class*="highlight"]',
            'p[class*="feature"]',
            'span[class*="feature"]',
            'li[class*="feature"]',
            'div[data-testid*="feature"]',
            'div[role="listitem"]'
        ]
        
        for selector in possible_selectors:
            try:
                elements = driver.find_elements(By.CSS_SELECTOR, selector)
                if elements:
                    print(f"\n  使用选择器 '{selector}' 找到 {len(elements)} 个元素:")
                    for i, element in enumerate(elements[:10]):  # 只显示前10个
                        try:
                            text = element.text.strip()
                            if text and len(text) > 5:  # 过滤掉太短的文本
                                print(f"    [{i+1}] {text}")
                        except:
                            pass
            except:
                pass
        
        # 方法3：查找包含特定文本的元素
        print("\n📋 方法3：查找包含已知Key Features文本的元素")
        known_features = [
            "16\" Alloy Wheels",
            "13.2\" Touchscreen",
            "Keyless Entry",
            "LED Headlamps",
            "Quickclear heated windscreen"
        ]
        
        for feature in known_features:
            try:
                elements = driver.find_elements(By.XPATH, f"//*[contains(text(), '{feature}')]")
                if elements:
                    print(f"\n  包含 '{feature}' 的元素:")
                    for i, element in enumerate(elements):
                        try:
                            text = element.text.strip()
                            tag_name = element.tag_name
                            class_attr = element.get_attribute('class') or ''
                            print(f"    [{i+1}] <{tag_name} class='{class_attr}'> {text}")
                        except:
                            pass
            except:
                pass
        
        # 方法4：截图保存当前页面状态
        print("\n📸 保存页面截图...")
        screenshot_path = "./release/key_features_debug.png"
        driver.save_screenshot(screenshot_path)
        print(f"✅ 截图已保存到: {screenshot_path}")
        
        # 方法5：保存页面HTML源码
        print("\n💾 保存页面HTML源码...")
        html_path = "./release/key_features_debug.html"
        with open(html_path, 'w', encoding='utf-8') as f:
            f.write(driver.page_source)
        print(f"✅ HTML源码已保存到: {html_path}")
        
    except Exception as e:
        print(f"❌ 诊断过程中出现异常: {e}")
        import traceback
        traceback.print_exc()
        
    finally:
        driver.quit()
        print("\n🧹 WebDriver已关闭")

if __name__ == "__main__":
    diagnose_key_features_extraction()
